﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Security.Authorizations
{
    [DataContract(Name = "Permissions", Namespace = "http://schemas.authorizations.org/2010/12/Permissions")]
    public class Permissions
    {

        List<string> _permissions;

        public Permissions()
        {
            _permissions = new List<string>();
        }

        public const string read = "read";
        public const string update = "update";
        public const string create = "create";
        public const string delete = "delete";
        public const string execute = "execute";

        [DataMember(Name = read)]
        public bool canRead
        {
            get
            {
                return getPermission(read);
            }
            internal set
            {
                setPermission(value, read);
            }
        }

        [DataMember(Name = update)]
        public bool canUpdate
        {
            get
            {
                return getPermission(update);
            }
            internal set
            {
                setPermission(value, update);
            }
        }

        [DataMember(Name = create)]
        public bool canCreate
        {
            get
            {
                return getPermission(create);
            }
            internal set
            {
                setPermission(value, create);
            }
        }

        [DataMember(Name = delete)]
        public bool canDelete
        {
            get
            {
                return getPermission(delete);
            }
            internal set
            {
                setPermission(value, delete);
            }
        }

        [DataMember(Name = execute)]
        public bool canExecute
        {
            get
            {
                return getPermission(execute);
            }
            internal set
            {
                setPermission(value, execute);
            }
        }

        //public void addPermissions(

        [DataMember(Name = "extendedPermissions")]
        public virtual string[] extendedPermissions
        {
            get
            {
                return _permissions.ToArray();
            }

        }

        public void addPermissions(string[] permissions)
        {
            foreach (var permission in permissions)
            {
                setPermission(true, permission);    
            }            
        }

        public void addPermission(string permission)
        {
            setPermission(true, permission);
        }
        
        public void removePermissions(string[] permissions)
        {
            foreach (var permission in permissions)
            {
                setPermission(false, permission);
            }          
        }        

        public void removePermission(string permission)
        {
            setPermission(false, permission);
        }        

        private bool getPermission(string perm)
        {
            if (_permissions.Contains(perm))
                return true;
            else
                return false;
        }
        private void setPermission(bool value, string perm)
        {
            if (value)
            {
                if (!_permissions.Contains(perm))
                    _permissions.Add(perm);

            }
            else
                if (_permissions.Contains(perm))
                    _permissions.Remove(perm);
        }

    } 
}
